package com.jlwl.houseservice.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.Article;
import com.jlwl.houseservice.entity.ArticleCategory;
import com.jlwl.houseservice.service.ArticleCategoryService;
import com.jlwl.houseservice.service.ArticleService;
import com.jlwl.houseservice.vo.ArticleDetailVO;
import com.jlwl.houseservice.vo.ArticleQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员文章控制器
 */
@RestController
@RequestMapping("/admin/articles")
public class AdminArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCategoryService articleCategoryService;

    /**
     * 获取文章列表
     */
    @GetMapping
    public Result<Page<Article>> list(ArticleQueryVO queryVO) {
        Page<Article> page = articleService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public Result<ArticleDetailVO> getDetail(@PathVariable Integer id) {
        ArticleDetailVO detailVO = articleService.getArticleDetail(id);
        if (detailVO == null) {
            return Result.error("文章不存在");
        }
        return Result.success(detailVO);
    }

    /**
     * 创建文章
     */
    @PostMapping
    public Result<Void> create(@RequestBody Article article) {
        articleService.createArticle(article);
        return Result.success();
    }

    /**
     * 更新文章
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Integer id, @RequestBody Article article) {
        article.setId(id);
        boolean success = articleService.updateArticle(article);
        if (!success) {
            return Result.error("文章不存在");
        }
        return Result.success();
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean success = articleService.deleteArticle(id);
        if (!success) {
            return Result.error("文章不存在");
        }
        return Result.success();
    }

    /**
     * 获取所有文章分类
     */
    @GetMapping("/categories")
    public Result<List<ArticleCategory>> getAllCategories() {
        List<ArticleCategory> list = articleCategoryService.getAllEnabledCategories();
        return Result.success(list);
    }

    /**
     * 获取分类列表（分页）
     */
    @GetMapping("/categories/page")
    public Result<Page<ArticleCategory>> getCategoryList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status) {
        Page<ArticleCategory> pageData = articleCategoryService.pageList(page, size, status);
        return Result.success(pageData);
    }

    /**
     * 创建分类
     */
    @PostMapping("/categories")
    public Result<Void> createCategory(@RequestBody ArticleCategory category) {
        articleCategoryService.createCategory(category);
        return Result.success();
    }

    /**
     * 更新分类
     */
    @PutMapping("/categories/{id}")
    public Result<Void> updateCategory(@PathVariable Integer id, @RequestBody ArticleCategory category) {
        category.setId(id);
        boolean success = articleCategoryService.updateCategory(category);
        if (!success) {
            return Result.error("分类不存在");
        }
        return Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/categories/{id}")
    public Result<Void> deleteCategory(@PathVariable Integer id) {
        boolean success = articleCategoryService.deleteCategory(id);
        if (!success) {
            return Result.error("分类不存在");
        }
        return Result.success();
    }
} 